package com.hui.security.sys.service.impl;

import com.hui.security.sys.entity.User;
import com.hui.security.sys.exception.ExceptionEnum;
import com.hui.security.sys.exception.UserException;
import com.hui.security.sys.mapper.UserMapper;
import com.hui.security.sys.service.RoleService;
import com.hui.security.sys.service.UserService;
import com.hui.security.sys.vo.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <b><code>UserServiceImpl</code></b>
 * <p/>
 * Description:
 * <p/>
 * <b>Creation Time:</b> 2019/1/5 0:27.
 *
 * @author HuWeihui
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleService roleService;

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @Override
    public User add(User user) {
        //通过账号和电话号码判断没有重复账号
        if (null != userMapper.selectByMobileAndAccount(user.getUserAccount(), user.getUserMobile())) {
            throw new UserException(ExceptionEnum.USER_ERROR_REPEAT_USER);
        }
        userMapper.insertSelective(user);
        return user;
    }

    /**
     * 删除用户
     *
     * @param userId
     */
    @Override
    public void delete(String userId) {
        userMapper.deleteByPrimaryKey(userId);
        //删除用户相关角色
        roleService.deleteByUserId(userId);
    }

    /**
     * 更新用户
     *
     * @param user
     */
    @Override
    public void update(User user) {
        //通过账号和电话号码判断没有重复账号
        User repeatUser = userMapper.selectByMobileAndAccount(user.getUserAccount(), user.getUserMobile());
        if (!repeatUser.getUserId().equals(user.getUserId())) {
            throw new UserException(ExceptionEnum.USER_ERROR_REPEAT_USER);
        }
        userMapper.updateByPrimaryKeySelective(user);
    }

    /**
     * 查询用户
     *
     * @param userId
     * @return
     */
    @Override
    public User get(String userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    /**
     * 用户登录
     *
     * @param loginVO
     * @return
     */
    @Override
    public User userLogin(LoginVO loginVO) {
        User user = userMapper.selectByUserAccount(loginVO.getUserAccount());
        return user;
    }

    /**
     * 通过账号查询用户
     *
     * @param account
     * @return
     */
    @Override
    public User getByUserAccount(String account) {
        return userMapper.selectByUserAccount(account);
    }

    /**
     * 查询用户组下的组员
     *
     * @param groupId
     * @return
     */
    @Override
    public List<User> listByGroupId(String groupId) {
        return userMapper.selectByGroupId(groupId);
    }

    /**
     * 删除角色相关的用户
     *
     * @param roleId
     */
    @Override
    public void deleteRefByRoleId(String roleId) {
        userMapper.deleteRefByRoleId(roleId);
    }

}
